#include "kinectargamelogic.h"


#include <cv.h>
#include <highgui.h>


#include "KinectWinMgr.h"
#include "trackingsystem.h"


KinectARGameLogic::KinectARGameLogic(int _camWidth, int _camHeight)
	: GameLogic(),
	mCamWidth(_camWidth),
	mCamHeight(_camHeight)
{

}


KinectARGameLogic::~KinectARGameLogic()
{
	if (mKinectMgr)
	{
		mKinectMgr->NuiUnInit();
		delete mKinectMgr;
		mKinectMgr = 0;
	}

	if (mTrackingSystem)
	{
		delete mTrackingSystem;
		mTrackingSystem = 0;
	}

	if (mTexData)
	{
		delete [] mTexData;
		mTexData = 0;
	}
}

void KinectARGameLogic::Init()
{
	mKinectMgr = new KinectWinMgr();
	mKinectMgr->NuiInit();
	mKinectMgr->SetEnableDrawSkeleton(true);

	mTrackingSystem = new TrackingSystem();
	mTrackingSystem->Init(mCamWidth, mCamHeight);

	mTexData = new unsigned char[mCamWidth * mCamHeight * 3];
	memset(mTexData, 0, sizeof(unsigned char) * mCamWidth * mCamHeight * 3);

	mTrackingInterval = 0;
}

void KinectARGameLogic::Update(float _elapseTime)
{
	mTrackingInterval += _elapseTime;
	if (mTrackingInterval > 1.f)
	{
		mTrackingInterval = 0.f;
		//if (mKinectMgr->IsSkeletonFound())
		memcpy(mTexData, mKinectMgr->GetSkeletonTex(),
			sizeof(unsigned char) * mCamWidth * mCamHeight * 3);

		/*IplImage* img = cvCreateImage(cvSize(mCamWidth, mCamHeight), 8, 3);
		memcpy(img->imageData, mTexData, mCamWidth * mCamHeight * 3);
		cvSaveImage("test_kinect.png", img);
		cvReleaseImage(&img);*/

		printf("%d\n", mTrackingSystem->Update(mTexData));

	}

}

void KinectARGameLogic::Render()
{
	
}